package main

import "fmt"

// list int del v = 4 first

func main() {
	list := constructList([]int{1, 2, 3, 4, 1, 2, 3, 4})
	printList(list)

	fmt.Println("------------------")
	delListElement(list, 3)
	printList(list)
}

func printList(list *ListNodeDto) {
	for list != nil {
		fmt.Println(list.Val)
		list = list.Next
	}
}

func constructList(valList []int) *ListNodeDto {
	head := &ListNodeDto{}
	p := head
	for _, v := range valList {
		p.Next = &ListNodeDto{Val: v}
		p = p.Next
	}
	return head.Next
}

type ListNodeDto struct {
	Next *ListNodeDto
	Val  int
}

func delListElement(list *ListNodeDto, val int) {
	if list == nil {
		return
	}
	curr := list
	prev := &ListNodeDto{Next: list}

	delMap := map[int]bool{}
	for curr != nil {
		if curr.Val == val && !delMap[curr.Val] {
			prev.Next = curr.Next
			delMap[curr.Val] = true
		}
		prev = curr
		curr = curr.Next
	}
}
